<template>

  <van-nav-bar
      title="排行榜"
      left-text="返回"
      left-arrow
      @click-left="onClickFan"
  />


  <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
  >
    <van-cell
        v-for="(item, index) in list"
        :key="index"
        :title="item.username"
        :is-link="true"
        :value="`${item.number}个问题`"
        :icon="item.imageUrl"
        class="user-item"
    />
  </van-list>
</template>

<script setup>
import { ref,onMounted } from 'vue';
import {get,post} from "../../util/request"; // 确保正确导入 Toast


const list = ref([]);
const loading = ref(false);
const finished = ref(false);



const onClickFan = () => history.back();

onMounted(() => {
  fetch();
})

const fetch = async () => {
  try {
    loading.value = true;
    const response = await get('circle/list/selectAll');
    if (response.code === 0) {
      list.value = response.data



      // 假设response.data.data是一个数组，且每个item都包含userId
      for (const item of list.value) {
        await fetchUserData(item.userId);
      }

    } else {
      console.error('请求失败，状态码：', response.status);
    }
  } catch (error) {
    console.error('请求出错：', error);
  } finally {
    loading.value = false;
    finished.value = true;
  }
};


// 新增函数用于获取用户信息
const fetchUserData = async (userId) => {
  try {
    const userResponse = await get(`user/user/selectUser/${userId}`);
    if (userResponse.code === 0) {
      // 根据需要处理用户信息，例如更新list中的用户详情
      // 注意：此处仅为示例，具体实现需根据实际需求调整
      const userData = userResponse.data;
      // 假设找到对应item并更新
      const listItem = list.value.find(item => item.userId === userId);
      if (listItem) {
        Object.assign(listItem, userData);
      }
    } else {
      console.error(`获取用户信息失败，userId: ${userId}, 状态码：`, userResponse.data.code);
    }
  } catch (error) {
    console.error(`获取用户信息出错，userId: ${userId}`, error);
  }
};

</script>
<style>
.user-item {
  padding: 10px;
  border-bottom: 1px solid #f7f7f7;
  display: flex;
  align-items: center;
}

.user-item img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
}

.user-item .van-cell__value {
  font-size: 14px;
  color: #999;
}
</style>